package com.example.myapplication.ui.news.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.myapplication.R;
import com.example.myapplication.app.AppConstant;
import com.example.myapplication.base.BaseMvpFragment;
import com.example.myapplication.model.bean.NewsSummary;
import com.example.myapplication.presenter.NewPresenter;
import com.example.myapplication.presenter.contract.NewContract;
import com.example.myapplication.ui.Home.adapter.NewAdapter;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;


public class NewsFrament extends BaseMvpFragment<NewPresenter> implements NewContract.View {

    @BindView(R.id.refresh)
    TwinklingRefreshLayout mRefresh;

    @BindView(R.id.rc_view)
    RecyclerView irc;

    private String mNewsId;
    private String mNewsType;
    private int mStartPage=0;
    private NewAdapter adapter;

    @Override
    protected NewPresenter initPresenter() {
        return new NewPresenter(getActivity());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news;
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            mNewsId = getArguments().getString(AppConstant.NEWS_ID);
            mNewsType = getArguments().getString(AppConstant.NEWS_TYPE);
        }
         adapter =new NewAdapter(new ArrayList());
        irc.setLayoutManager(new LinearLayoutManager(getContext()));
        irc.setAdapter(adapter);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        SinaRefreshView headerView = new SinaRefreshView(getActivity());
        headerView.setArrowResource(R.drawable.arrow);
        headerView.setTextColor(0xff745D5C);
        mRefresh.setHeaderView(headerView);
        LoadingView loadingView = new LoadingView(getActivity());
        mRefresh.setBottomView(loadingView);
        mRefresh.startRefresh();
        mRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                mStartPage=0;
                mPresenter.requestDataRefresh(mNewsType,mNewsId,mStartPage);
            }
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                mStartPage+=20;
                mPresenter.requestDataLoadMore(mNewsType,mNewsId,mStartPage);
            }
        });
    }

    @Override
    public void showError(String msg) {
        mRefresh.finishLoadmore();
        mRefresh.finishRefreshing();
    }

    @Override
    protected void initEventAndData() {
    }


    @Override
    public void SuccessRefresh(List<NewsSummary> newsSummaries) {
        adapter.setNewData(newsSummaries);
        mRefresh.finishRefreshing();
    }

    @Override
    public void SuccessLoadMore(List<NewsSummary> newsSummaries) {
        adapter.addData(newsSummaries);
        mRefresh.finishLoadmore();
    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
